package com.example.peanutsubway.dao.impl;

import com.example.peanutsubway.dao.GameTypeInfoDao;
import com.example.peanutsubway.entity.GameTypeInfo;
import com.example.peanutsubway.entity.PageInfo;
import com.example.peanutsubway.util.JDBCUtils;

import java.util.List;

/**
 * @Author：liyangsong
 * @Version：1.0
 * @Date：2021/10/11-21:35
 * @Since:jdk1.8
 * @Description:
 */


public class GameTypeDaoImpl implements GameTypeInfoDao {
    @Override
    public List<GameTypeInfo> typelist() {
        String sql = "select * from pn_game_type";
        return JDBCUtils.find(sql, GameTypeInfo.class);
    }

    @Override
    public List<GameTypeInfo> showAllType(GameTypeInfo gameTypeInfo, PageInfo pageInfo) {
        StringBuilder sql = new StringBuilder("select * from pn_game_type where des = 1");
        if (gameTypeInfo != null) {
            if (gameTypeInfo.getTypeName() != null && !"".equals(gameTypeInfo.getTypeName())) {
                sql.append(" and typeName like '%" + gameTypeInfo.getTypeName() + "%'");
            }
        }
        sql.append(" limit ?,?");
        return JDBCUtils.find(sql.toString(), GameTypeInfo.class, pageInfo.getStartIndex(), pageInfo.getPageNum());
    }


    @Override
    public Integer count(GameTypeInfo gameTypeInfo) {
        StringBuilder sql = new StringBuilder("select count(1) from pn_game_type where des = 1") ;
        if (gameTypeInfo != null) {
            if (gameTypeInfo.getTypeName() != null && !"".equals(gameTypeInfo.getTypeName())) {
                sql.append(" and typeName like '%" + gameTypeInfo.getTypeName() + "%'");
            }
        }
        return JDBCUtils.count(sql.toString()).intValue();
    }

    @Override
    public Boolean addGameType(GameTypeInfo gameTypeInfo) {
        String sql = "INSERT INTO pn_game_type VALUES(NULL,?,1)";
        return JDBCUtils.insert(sql, gameTypeInfo.getTypeName()) > 0;
    }

    @Override
    public Boolean updateGameType(GameTypeInfo gameTypeInfo) {
        String sql = "update pn_game_type set typename = ? where typeid = ? ";
        return JDBCUtils.update(sql, gameTypeInfo.getTypeName(), gameTypeInfo.getTypeId()) > 0;
    }

    @Override
    public Boolean deleteGameType(Integer typeId) {
        String sql = "update pn_game_type set des = 0 where typeid = ?";
        return JDBCUtils.update(sql, typeId) > 0;
    }
}
